package com.zjk.web;

import com.zjk.domain.Tag;
import com.zjk.service.BlogService;
import com.zjk.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * @author zhujinkai
 * @version 1.0
 * @date 2020/10/25 17:21
 */
@Controller
public class TagShowController {

    @Autowired
    public TagService tagService;

    @Autowired
    public BlogService blogService;

    @GetMapping("/tags/{id}")
    public String tags(@PageableDefault(size = 8, sort = {"updateTime"}, direction = Sort.Direction.DESC)
                                Pageable pageable,
                        @PathVariable("id") Long id,
                        Model model) {
        //size足够大，就可以显示所有分类名称，且按文章数排序。
        List<Tag> tags = tagService.listTagTop(1000);
        if (id == -1) {
            id = tags.get(0).getId();
        }
        model.addAttribute("tags", tags);
        model.addAttribute("page", blogService.listBlog(id, pageable));
        model.addAttribute("activeTagId", id);
        return "tags";
    }
}
